package com.atguigu;

import com.atguigu.bean.UserBean;
import org.apache.spark.SparkConf;
import org.apache.spark.sql.*;

/**
 * @author yhm
 * @create 2022-12-26 10:38
 */
public class Test06_JSON {
    public static void main(String[] args) {
        // 1. 创建sparkConf配置对象
        SparkConf conf = new SparkConf().setAppName("sql").setMaster("local[*]");

        // 2. 创建sparkSession连接对象
        SparkSession spark = SparkSession.builder().config(conf).getOrCreate();

        // 3. 编写代码
        Dataset<Row> json = spark.read().json("input/user.json");
        json.printSchema();

        Dataset<UserBean> beanDataset = json.as(Encoders.bean(UserBean.class));
        beanDataset.show();

        // 写出为json文件
        beanDataset.write()
                .mode(SaveMode.Overwrite)
                .json("output");

        // 4. 关闭sparkSession
        spark.close();
    }
}
